<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医生" prop="doctorId">
        <el-select v-model="queryParams.doctorId" placeholder="请选择医生" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="就诊人" prop="patientId">
        <el-select v-model="queryParams.patientId" placeholder="请选择就诊人" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable filterable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker v-model="daterangeBuyTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="购买人" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择购买人" clearable filterable size="small">
          <el-option v-for="dict in userIdOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付类型" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付类型" clearable filterable size="small">
          <el-option v-for="dict in payTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付流水" prop="payNo">
        <el-input v-model="queryParams.payNo" placeholder="请输入支付流水" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" clearable filterable size="small">
          <el-option v-for="dict in payStatusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['order:medicineOrder:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['order:medicineOrder:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['order:medicineOrder:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['order:medicineOrder:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="medicineOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="医生" align="center" prop="doctorId" />
      <el-table-column label="就诊人" align="center" prop="patientId" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="肝功能" align="center" prop="liverFunction" :formatter="liverFunctionFormat" />
      <el-table-column label="肾功能" align="center" prop="renalFunction" :formatter="renalFunctionFormat" />
      <el-table-column label="过敏史" align="center" prop="allergicHistory" :formatter="allergicHistoryFormat" />
      <el-table-column label="生育状态" align="center" prop="fertilityStatus" :formatter="fertilityStatusFormat" />
      <el-table-column label="订单状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="下单时间" align="center" prop="buyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买人" align="center" prop="userId" :formatter="userIdFormat" />
      <el-table-column label="应付总金额" align="center" prop="total" />
      <el-table-column label="支付类型" align="center" prop="payType" :formatter="payTypeFormat" />
      <el-table-column label="支付流水" align="center" prop="payNo" />
      <el-table-column label="支付状态" align="center" prop="payStatus" :formatter="payStatusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:medicineOrder:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['order:medicineOrder:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改开药申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医生" prop="doctorId">
          <el-select v-model="form.doctorId" placeholder="请选择医生">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="就诊人" prop="patientId">
          <el-select v-model="form.patientId" placeholder="请选择就诊人">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="所患疾病" prop="sick">
          <el-input v-model="form.sick" placeholder="请输入所患疾病" />
        </el-form-item>
        <el-form-item label="肝功能" prop="liverFunction">
          <el-select v-model="form.liverFunction" placeholder="请选择肝功能">
            <el-option v-for="dict in liverFunctionOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="肾功能" prop="renalFunction">
          <el-select v-model="form.renalFunction" placeholder="请选择肾功能">
            <el-option v-for="dict in renalFunctionOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="过敏史" prop="allergicHistory">
          <el-select v-model="form.allergicHistory" placeholder="请选择过敏史">
            <el-option v-for="dict in allergicHistoryOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生育状态" prop="fertilityStatus">
          <el-select v-model="form.fertilityStatus" placeholder="请选择生育状态">
            <el-option v-for="dict in fertilityStatusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="病情描述" prop="sickDesc">
          <el-input v-model="form.sickDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="患者上传处方">
          <imageUpload v-model="form.oldPrescribe" />
        </el-form-item>
        <el-form-item label="医生上传处方">
          <imageUpload v-model="form.newPrescribe" />
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择订单状态">
            <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下单时间" prop="buyTime">
          <el-date-picker clearable size="small" v-model="form.buyTime" type="date" value-format="yyyy-MM-dd" placeholder="选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="购买人" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择购买人">
            <el-option v-for="dict in userIdOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应付总金额" prop="total">
          <el-input v-model="form.total" placeholder="请输入应付总金额" />
        </el-form-item>
        <el-form-item label="支付类型" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择支付类型">
            <el-option v-for="dict in payTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付流水" prop="payNo">
          <el-input v-model="form.payNo" placeholder="请输入支付流水" />
        </el-form-item>
        <el-form-item label="支付状态" prop="payStatus">
          <el-select v-model="form.payStatus" placeholder="请选择支付状态">
            <el-option v-for="dict in payStatusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMedicineOrder, getMedicineOrder, delMedicineOrder, addMedicineOrder, updateMedicineOrder, exportMedicineOrder } from "@/api/order/medicineOrder";
import { listDoctorInfo } from "@/api/doctor/doctorInfo";
import { listPatientsInfo } from "@/api/patients/patientsInfo";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "MedicineOrder",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 开药申请表格数据
      medicineOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 肝功能字典
      liverFunctionOptions: [],
      // 肾功能字典
      renalFunctionOptions: [],
      // 过敏史字典
      allergicHistoryOptions: [],
      // 生育状态字典
      fertilityStatusOptions: [],
      // 订单状态字典
      statusOptions: [],
      // 下单时间时间范围
      daterangeBuyTime: [],
      // 购买人字典
      userIdOptions: [],
      // 支付类型字典
      payTypeOptions: [],
      // 支付状态字典
      payStatusOptions: [],
      doctorOptions: [],
      patientsOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorId: null,
        patientId: null,
        orderNo: null,
        status: null,
        buyTime: null,
        userId: null,
        total: null,
        payType: null,
        payNo: null,
        payStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("liver_function").then(response => {
      this.liverFunctionOptions = response.data;
    });
    this.getDicts("renal_function").then(response => {
      this.renalFunctionOptions = response.data;
    });
    this.getDicts("allergic_history").then(response => {
      this.allergicHistoryOptions = response.data;
    });
    this.getDicts("fertility_status").then(response => {
      this.fertilityStatusOptions = response.data;
    });
    this.getDicts("order_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getSysUserList().then(res => {
      this.userIdOptions = [];
      let list = res.rows;
      list.forEach(element => {
        this.userIdOptions.push({
          dictValue: element.userId,
          dictLabel: element.userName,
        });
      });
    });
    this.getDicts("payment_type").then(response => {
      this.payTypeOptions = response.data;
    });
    this.getDicts("pay_status").then(response => {
      this.payStatusOptions = response.data;
    });
  },
  methods: {
    getDoctorOptions() {
      listDoctorInfo().then(res => {
        let list = res.rows;
        this.doctorOptions = [];
        list.forEach(item => {
          this.doctorOptions.push({
            dictValue: item.id;
            dictValue: item.name;
          })
        })
      })
    },
    /** 查询开药申请列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeBuyTime && '' != this.daterangeBuyTime) {
        this.queryParams.params["beginBuyTime"] = this.daterangeBuyTime[0];
        this.queryParams.params["endBuyTime"] = this.daterangeBuyTime[1];
      }
      listMedicineOrder(this.queryParams).then(response => {
        this.medicineOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 肝功能字典翻译
    liverFunctionFormat(row, column) {
      return this.selectDictLabel(this.liverFunctionOptions, row.liverFunction);
    },
    // 肾功能字典翻译
    renalFunctionFormat(row, column) {
      return this.selectDictLabel(this.renalFunctionOptions, row.renalFunction);
    },
    // 过敏史字典翻译
    allergicHistoryFormat(row, column) {
      return this.selectDictLabel(this.allergicHistoryOptions, row.allergicHistory);
    },
    // 生育状态字典翻译
    fertilityStatusFormat(row, column) {
      return this.selectDictLabel(this.fertilityStatusOptions, row.fertilityStatus);
    },
    // 订单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 购买人字典翻译
    userIdFormat(row, column) {
      return this.selectDictLabel(this.userIdOptions, row.userId);
    },
    // 支付类型字典翻译
    payTypeFormat(row, column) {
      return this.selectDictLabel(this.payTypeOptions, row.payType);
    },
    // 支付状态字典翻译
    payStatusFormat(row, column) {
      return this.selectDictLabel(this.payStatusOptions, row.payStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        doctorId: null,
        patientId: null,
        orderNo: null,
        sick: null,
        liverFunction: null,
        renalFunction: null,
        allergicHistory: null,
        fertilityStatus: null,
        sickDesc: null,
        oldPrescribe: null,
        newPrescribe: null,
        status: null,
        buyTime: null,
        userId: null,
        total: null,
        payType: null,
        payNo: null,
        payStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeBuyTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加开药申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMedicineOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改开药申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMedicineOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMedicineOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除开药申请编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delMedicineOrder(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有开药申请数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportMedicineOrder(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
